import pyglet
from pyglet.image import SolidColorImagePattern
import os

# 注意：一个文件为一个 Python模块，要用的是里面的类
from SceneManager import SceneManager
from StartScene import StartScene
from GameScene import GameScene
from SaveScene import SaveScene
from SettingScene import SettingScene


class GameWindow(pyglet.window.Window):
    """主游戏窗口"""

    def __init__(self):
        # 是否全屏
        super().__init__(caption="xuan",fullscreen=False)

        # 创建场景管理器
        self.scene_manager = SceneManager(self)

        # 注册场景
        self.scene_manager.add_scene("start", StartScene)
        self.scene_manager.add_scene("game", GameScene)
        self.scene_manager.add_scene("save", SaveScene)
        self.scene_manager.add_scene("setting", SettingScene)
        
        # 设置初始场景
        self.scene_manager.switch_scene("start")
        # self.scene_manager.switch_scene("setting")

    def on_draw(self):
        """窗口绘制事件"""
        if self.scene_manager.current_scene:
            self.scene_manager.current_scene.on_draw()

    def on_key_press(self, symbol, modifiers):
        """窗口键盘事件"""
        if self.scene_manager.current_scene:
            # 将键盘事件传递给当前场景
            if hasattr(self.scene_manager.current_scene, 'on_key_press'):
                self.scene_manager.current_scene.on_key_press(symbol, modifiers)

    def on_mouse_motion(self, x: int, y: int, dx: int, dy: int):
        if self.scene_manager.current_scene:
            if hasattr(self.scene_manager.current_scene, 'on_mouse_motion'):
                self.scene_manager.current_scene.on_mouse_motion(x, y, dx, dy)

def main():
    """主函数"""
    #resoucess_load()
    global pressed_img, depressed_img
    pressed_img = SolidColorImagePattern((100, 226, 253, 255)).create_image(200, 50)
    depressed_img = SolidColorImagePattern((255, 0, 0, 255)).create_image(200, 50)

    # 创建存档文件夹
    folder_name = "saves"
    if not os.path.exists(folder_name):
        os.makedirs(folder_name)

    window = GameWindow()
    pyglet.app.run()


if __name__ == "__main__":
    main()
